package com.bristua.barcode.supplier.biz;

import com.bristua.barcode.common.bean.PageBean;
import com.bristua.barcode.supplier.dao.TQrCodeMapper;
import com.bristua.barcode.supplier.dao.TSupplierMapper;
import com.bristua.barcode.supplier.model.TQrCode;
import com.bristua.barcode.supplier.model.TSupplier;
import com.bristua.barcode.supplier.model.condition.TQrCodeExample;
import com.bristua.barcode.supplier.model.vo.QuerySupplierQRCodeVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2019/8/6 0006.
 */
@Service
public class QuerySupplierQRCodeBiz {

    @Resource
    private TQrCodeMapper tQrCodeMapper;

    @Resource
    private TSupplierMapper tSupplierMapper;

    public List<TQrCode> queryQrcode(String supplierId) {
        TQrCodeExample tQrCodeExample = new TQrCodeExample();
        tQrCodeExample.createCriteria().andSupplierIdEqualTo(supplierId);
        return tQrCodeMapper.selectByExample(tQrCodeExample);
    }

    public PageBean<QuerySupplierQRCodeVO>  queryQrcode(Integer isbind,Integer pageSize,Integer pageNo) {
        TQrCodeExample tQrCodeExample = new TQrCodeExample();
        TQrCodeExample.Criteria criteria = tQrCodeExample.createCriteria();
        criteria.andStatusEqualTo(1);
        if(isbind.intValue()==0 || isbind.intValue()==1){
            criteria.andIsBindEqualTo(isbind);
        }

        Page<TQrCode> objects = PageHelper.startPage(pageNo,pageSize);
        List<TQrCode> tQrCodes = tQrCodeMapper.selectByExample(tQrCodeExample);

        PageBean data = new PageBean<>(objects);
        if(data.getList()!=null && data.getList().size()>0){
            List<QuerySupplierQRCodeVO> querySupplierQRCodeVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TQrCode tQrCode = (TQrCode)data.getList().get(i);
                QuerySupplierQRCodeVO querySupplierQRCodeVO = new QuerySupplierQRCodeVO();
                BeanUtils.copyProperties(tQrCode, querySupplierQRCodeVO);
                TSupplier tSupplier = tSupplierMapper.selectByPrimaryKey(tQrCode.getSupplierId());
                if (tSupplier != null) {
                    querySupplierQRCodeVO.setSupplierName(tSupplier.getSupplierName());
                }
                querySupplierQRCodeVOS.add(querySupplierQRCodeVO);
            }
            data.getList().clear();
            data.getList().addAll(querySupplierQRCodeVOS);
            return data;
        }
        return null;
    }
}
